﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.OleDb;
using System.Linq;
using System.Text;
using System.Data;

namespace BBS.Data.Forum
{
    public class RuleTable
    {
        private static string _connstr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        private BaseTable _table = new BaseTable(_connstr, "Rule");
        /// <summary>
        /// 返回第一行设置记录
        /// </summary>
        /// <returns>第一行数据，如果没有数据返回null</returns>
        public DataRow GetTopRule()
        {
            if (_table.SelectTable().Rows.Count == 0)
            {
                return null;
            }
            return _table.SelectTable().Rows[0];
        }
        /// <summary>
        /// 插入记录
        /// </summary>
        /// <param name="rule">规则实体</param>
        /// <returns>受影响行数</returns>
        public int Insert(Entity.Rule rule)
        {
            string sql = string.Format("insert into [rule] ({0}) values ({1})", Filed.RULE_FILED,
                                       Filed.GetParametersString(Filed.RULE_FILED));
           return  _table.ExecuteNonQuery(sql, new OleDbParameter("@post_points", rule.PostPoints),
                                   new OleDbParameter("@reply_points", rule.ReplyPoints),
                                   new OleDbParameter("@top_points", rule.TopPoints),
                                   new OleDbParameter("@highlight_points", rule.HighlightPoints),
                                   new OleDbParameter("@login_points", rule.LoginPoints),
                                   new OleDbParameter("@move_points", rule.MovePoints),
                                   new OleDbParameter("@lock_points", rule.LockPoints),
                                   new OleDbParameter("@delete_points", rule.DeletePoints));
        }
        /// <summary>
        /// 更新记录
        /// </summary>
        /// <param name="rule">规则实体</param>
        /// <returns>受影响行数</returns>
        public int Update(Entity.Rule rule)
        {
            string sql = string.Format("UPDATE [rule] set  {0}", Filed.RULE_UPDATE_FILED);
            return _table.ExecuteNonQuery(sql, new OleDbParameter("@post_points", rule.PostPoints),
                                    new OleDbParameter("@reply_points", rule.ReplyPoints),
                                    new OleDbParameter("@top_points", rule.TopPoints),
                                    new OleDbParameter("@highlight_points", rule.HighlightPoints),
                                    new OleDbParameter("@login_points", rule.LoginPoints),
                                    new OleDbParameter("@move_points", rule.MovePoints),
                                    new OleDbParameter("@lock_points", rule.LockPoints),
                                    new OleDbParameter("@delete_points", rule.DeletePoints));
        }
    }
}
